﻿namespace FFF.Test.YCmd.Tests
{
    using Abp.EntityFrameworkCore.Configuration;
    using Abp.Modules;
    using Abp.Reflection.Extensions;
    using Castle.MicroKernel.Registration;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Configuration;
    using FFF.Test.Application.Framework;
    using FFF.Test.DataAccess.Framework;
    using FFF.Test.DataAccess.Framework.Configuration;
    using FFF.Test.Util.Configuration;
    using FFF.Test.Util.Consts;
    using FFF.Test.Util.Denpends;
    using FFF.Test.Util.Web;

    [DependsOn(typeof(YudmAbpApplicationModule))]
    public class AdbTestModule : AbpModule
    {
        public override void PreInitialize()
        {
            // 初始化config的配置为了读取appjson文件
            IConfigurationRoot configuration = AppConfigurationHelper.Get(Server.ContentRootPath);
            AppConfigConsts.InitConfigBySettingJson(configuration);

            base.IocManager.IocContainer.Register(Component.For<IConfiguration>().UsingFactoryMethod(x => configuration));

            base.IocManager.Register<IHttpContextAccessor, HttpContextForDenpend>();

            this.Configuration.Modules.AbpEfCore().AddDbContext<MyDbContext>(options =>
            {
                if (options.ExistingConnection != null)
                {
                    DbContextOptionsConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
                }
                else
                {
                    DbContextOptionsConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
                }
            });
        }

        public override void Initialize()
        {
            System.Reflection.Assembly thisAssembly = typeof(AdbTestModule).GetAssembly();
            IocManager.RegisterAssemblyByConvention(thisAssembly);
        }
    }
}
